package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.FrashPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.FrashDataBean;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.BottomDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.FUNCTIONS_FRASH)
public class FrashActivity extends BaseActivity<FrashPresenter> {

    List<String> coins = new ArrayList();
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.frash_record_iv)
    ImageView frashRecordIv;
    @BindView(R2.id.frash_hold_money_ll)
    LinearLayout frashHoldMoneyLl;
    @BindView(R2.id.frash_frash_money_ll)
    LinearLayout frashFrashMoneyLl;
    @BindView(R2.id.frash_hold_kind_tv)
    TextView frashHoldKindTv;
    @BindView(R2.id.frash_frash_kind_tv)
    TextView frashFrashKindTv;
    @BindView(R2.id.frash_hold_kind_next_tv)
    TextView frashHoldKindNextTv;
    @BindView(R2.id.frash_frash_kind_next_tv)
    TextView frashFrashKindNextTv;
    @BindView(R2.id.frash_hold_number)
    EditText frashHoldNumber;
    @BindView(R2.id.frash_frash_number)
    EditText frashFrashNumber;
    @BindView(R2.id.frash_exchange_tv)
    TextView frashExchangeTv;
    @BindView(R2.id.frash_all_money_tv)
    TextView frashAllMoneyTv;
    @BindView(R2.id.frash_bt)
    Button frashBt;
    @BindView(R2.id.frash_handing_tv)
    TextView tvShouxufei;
    private double ratio = 1;
    private double reRatio = 1;
    private String changType = "USDT";
    private boolean changeByUser;
    private String type = "AICASH";

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_frash;
    }

    @Override
    public FrashPresenter getPresenter() {
        return new FrashPresenter();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        frashHoldNumber.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(8)});
        frashFrashNumber.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(8)});
        setFrashData();
        tvTitle.setText(App.INSTANCE.getString(R.string.functions_frashActivity1));
        //editText监听
//        frashHoldNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                Log.e("hasFocus", hasFocus + "");
//                if (hasFocus) {
//                }
//            }
//        });
        frashHoldNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!changeByUser) {
                    if (s.length() != 0) {
                        changeByUser = true;
                        try {
                            //闪兑数量
                            frashFrashNumber.setText(CommonUtil.dealCoinNum(NumberUtil.keepNoZero(ArithUtil.mul(Double.valueOf(s.toString()), ratio)), getText(frashHoldKindTv)));
                            changeByUser = false;
                            //手续费
                            tvShouxufei.setText(App.INSTANCE.getString(R.string.functions_frashActivity2) + ArithUtil.div(ArithUtil.mul(Double.parseDouble(s.toString()), reRatio), 100, 4) + getText(frashHoldKindTv));
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    if (s.length() == 0) {
                        tvShouxufei.setText(App.INSTANCE.getString(R.string.functions_frashActivity2) + "0.00" + getText(frashHoldKindTv));
                        frashFrashNumber.setText("");
                    }
                }
            }
        });
        frashFrashNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!changeByUser) {
                    changeByUser = true;
                    if (s.length() == 0) {
                        frashHoldNumber.setText("");
                        tvShouxufei.setText(App.INSTANCE.getString(R.string.functions_frashActivity2) + "0.00" + getText(frashHoldKindTv));
                    } else {
                        try {
                            frashHoldNumber.setText(ArithUtil.div(Double.parseDouble(s.toString()), ratio, 4) + "");
                            if (!isEmpty(getText(frashHoldNumber))) {
                                tvShouxufei.setText(App.INSTANCE.getString(R.string.functions_frashActivity2) + ArithUtil.div(ArithUtil.mul(ArithUtil.div(Double.parseDouble(getText(frashHoldNumber)), ratio, 4), reRatio), 100, 4) + getText(frashHoldKindTv));
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    changeByUser = false;
                }
            }
        });
    }

    private void setFrashData() {
        coins.add(Constans.COIN_BTC);
        coins.add(Constans.COIN_ETH);
        coins.add(Constans.COIN_LTC);
        coins.add(Constans.COIN_USDT);
        coins.add(Constans.COIN_AICASH);
    }

    @Override
    public void initData() {
        //闪兑界面
        getCoinMsg("AICASH");
        /*闪兑记录*/
        ClickUtil.click(300, frashRecordIv, () -> {
            ARouter.getInstance().build(RConfig.FUNCTIONS_ROLL_RECORD).withString(Constans.TYPE, Constans.SHANDUI).navigation();
        });
        /*切换币种*/
        ClickUtil.click(300, frashHoldMoneyLl, () -> {
            BottomDialog.chooseCoins(this, coins, (coinName) -> {
                frashHoldNumber.getText().clear();
                frashFrashNumber.getText().clear();
                getCoinMsg(coinName.toLowerCase());
                frashHoldKindTv.setText(coinName);
                frashHoldKindNextTv.setText(coinName);
                tvShouxufei.setText(App.INSTANCE.getString(R.string.functions_frashActivity3) + coinName);
                type = coinName;
                if (coinName.equals(Constans.COIN_AICASH)) {
                    changType = Constans.COIN_USDT;
                    frashFrashKindTv.setText(Constans.COIN_USDT);
                    frashFrashKindNextTv.setText(Constans.COIN_USDT);
                } else {
                    changType = Constans.COIN_AICASH;
                    frashFrashKindTv.setText(Constans.COIN_AICASH);
                    frashFrashKindNextTv.setText(Constans.COIN_AICASH);
                }
            });
        });
        //确定兑换
        ClickUtil.click(300, frashBt, () -> {
            if (TextUtils.isEmpty(frashHoldNumber.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_frashActivity4));
                return;
            } else {
                if (TextUtils.isEmpty(frashFrashNumber.getText().toString())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_frashActivity5));
                    return;
                } else {
                    BottomDialog.posswordInput(this, tv -> {
                        mPresenter.enSureExchange(frashHoldKindTv.getText().toString(), tv, frashHoldNumber.getText().toString());
                    });
                }
            }

        });
    }

    private void getCoinMsg(String coinName) {
        mPresenter.getFrashData(coinName);
    }

    @SuppressLint("SetTextI18n")
    public void getFrashData(FrashDataBean data) {
        if (data != null) {
            frashExchangeTv.setText(1 + type + "=" + CommonUtil.dealCoinNum(String.valueOf(data.getExchange_rate()), getText(frashFrashKindNextTv)) + getText(frashFrashKindNextTv));
            if (!TextUtils.isEmpty(data.getUsable())) {
                frashAllMoneyTv.setText(NumberUtil.keep4(Double.parseDouble(data.getUsable())) + " " + type);
            } else {
                frashAllMoneyTv.setText("0.00 " + type);
            }
            ratio = Double.parseDouble(data.getExchange_rate() + "");
            reRatio = Double.parseDouble(data.getFlash_exchange());
        }
    }

    public void enSureExchangeSuccess() {
        ARouter.getInstance().build(RConfig.FUNCTIONS_ROLL_RECORD).withString(Constans.TYPE, Constans.SHANDUI).navigation();
    }

}
